package Socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by sunwei on 2017/9/29
 * Time:13:48
 */
/*
服务类，用来处理socket编程所需的服务
 */
public class SocketServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(4567);//设置服务器端的端口号
            System.out.println("ServerSocket Start:"+ serverSocket);//服务器端开启
            socket = serverSocket.accept();//客户端接受服务器端的请求
            System.out.println("得到客户端的地址"+socket.getInetAddress());
            //调用线程类的start方法，来启动线程
            new ServerSocketThread(socket).start();
        } catch (Exception e) {
           try {
               socket.close();//关闭客户端套接字
           } catch (IOException e1) {
               e1.printStackTrace();
           }
        }finally {
            try {
                serverSocket.close(); //关闭服务器端套接字
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
